cmake_minimum_required(VERSION 3.5)

project(QMLCmakeLanguage LANGUAGES CXX)

#设置编译选项
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

#定义函数translations_update_ts，使用lupdate命令更新.ts文件里面的翻译信息
function(TRANSLATIONS_UPDATE_TS supported_langs out_ts_files)
    find_program(LUPDATE_EXECUTABLE NAMES lupdate HINTS "${_qt_bin_dir}")
    find_package(Qt5LinguistTools)
    if (Qt5LinguistTools_FOUND)
        if(LINUX)
            execute_process(COMMAND awk -F= "/^NAME/{print $2}" /etc/os-release
                            OUTPUT_VARIABLE DISTR_NAME
                            OUTPUT_STRIP_TRAILING_WHITESPACE)
        endif()

        foreach(SUPPORTED_LANG ${supported_langs})
            set(TS_FILE "${CMAKE_CURRENT_SOURCE_DIR}/translations/${SUPPORTED_LANG}.ts")
            list(APPEND TS_FILES "${TS_FILE}")
            message("update: ${TS_FILE}")
            if (DISTR_NAME AND ("${DISTR_NAME}" STREQUAL "Fedora"))
                execute_process(COMMAND lupdate-qt5  ${CMAKE_CURRENT_SOURCE_DIR} -ts "${TS_FILE}")
            else()
                execute_process(COMMAND ${LUPDATE_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR} -ts "${TS_FILE}")
            endif()
        endforeach()

        set(${out_ts_files} ${TS_FILES} PARENT_SCOPE)
    else()
        message("Qt5LinguistTools not found, please check your QT5 instalation")
    endif()
endfunction()
#定义函数translations_compile_qm，使用lrelease命令将ts文件编译成qm文件
function(TRANSLATIONS_COMPILE_QM ts_files out_qm_files)
    message("translate ts files to qm files ${ts_files}")
    find_program(LRELEASE_EXECUTABLE NAMES lrelease HINTS "${_qt_bin_dir}")
    find_package(Qt5LinguistTools)
    if (Qt5LinguistTools_FOUND)
        if(LINUX)
            execute_process(COMMAND awk -F= "/^NAME/{print $2}" /etc/os-release
                            OUTPUT_VARIABLE DISTR_NAME
                            OUTPUT_STRIP_TRAILING_WHITESPACE)
        endif()
        foreach(TS_FILE ${ts_files})
            message("compile: ${TS_FILE}")
            get_filename_component(TS_FILE_WE ${TS_FILE} NAME_WE)
            set(QM_FILE_NAME "${TS_FILE_WE}.qm")
            list(APPEND QM_FILES "${QM_FILE_NAME}")
            if (DISTR_NAME AND ("${DISTR_NAME}" STREQUAL "Fedora"))
                execute_process(COMMAND lrelease-qt5 "${TS_FILE}" -qm "${CMAKE_CURRENT_SOURCE_DIR}/translations/${QM_FILE_NAME}")
            else()
                execute_process(COMMAND ${LRELEASE_EXECUTABLE} "${TS_FILE}" -qm "${CMAKE_CURRENT_SOURCE_DIR}/translations/${QM_FILE_NAME}")
            endif()
            message("compiled translation: ${CMAKE_CURRENT_SOURCE_DIR}/translations/${QM_FILE_NAME}")
        endforeach()

        set(${out_qm_files} ${QM_FILES} PARENT_SCOPE)
    else()
        message("Qt5LinguistTools not found, please check your QT5 instalation")
    endif()
endfunction()

#将支持的语言列表添加到数组里面
list(APPEND SUPPORTED_LANGS "en_US")
list(APPEND SUPPORTED_LANGS "zh_CN")

#更新TS文件
translations_update_ts("${SUPPORTED_LANGS}" TS_FILES)
translations_compile_qm("${TS_FILES}" QM_FILES)

#更新translations.qrc文件
set(TRANSLATIONS_QRC_FILE "${CMAKE_CURRENT_SOURCE_DIR}/translations.qrc")
file(WRITE "${TRANSLATIONS_QRC_FILE}" "<RCC>\n\t<qresource prefix=\"/\">\n")
foreach (QM_FILE ${QM_FILES})
    file(APPEND "${TRANSLATIONS_QRC_FILE}" "\t\t<file>translations/${QM_FILE}</file>\n")
endforeach ()
file(APPEND "${TRANSLATIONS_QRC_FILE}" "\t</qresource>\n</RCC>")

#查找依赖的Qt组建
find_package(Qt5 COMPONENTS Core Quick REQUIRED)
#查找macdeployqt命令
get_target_property(_qmake_executable Qt5::qmake IMPORTED_LOCATION)
get_filename_component(_qt_bin_dir "${_qmake_executable}" DIRECTORY)
find_program(DEPLOYQT_EXECUTABLE NAMES windeployqt macdeployqt HINTS "${_qt_bin_dir}")
#添加源文件
file(GLOB SRC_LIST "*.cpp")
file(GLOB HEADERS "*.h")
set(RESOURCE qml.qrc translations.qrc)

#打包成静态可执行文件
if(APPLE)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -framework AppKit -framework CoreFoundation -Wno-unused-command-line-argument")
    add_executable(${PROJECT_NAME} MACOSX_BUNDLE ${SRC_LIST} ${HEADERS} ${RESOURCE})
    configure_file(${CMAKE_SOURCE_DIR}/Info.plist.in ${CMAKE_BINARY_DIR}/Info.plist)
    set_target_properties(${PROJECT_NAME} PROPERTIES MACOSX_BUNDLE_INFO_PLIST ${CMAKE_BINARY_DIR}/Info.plist)
    add_custom_command(TARGET ${PROJECT_NAME}
        POST_BUILD
        COMMAND ${DEPLOYQT_EXECUTABLE} "$<TARGET_BUNDLE_DIR:${PROJECT_NAME}>" -qmldir=${CMAKE_SOURCE_DIR}
    )
elseif(WIN32)
    add_executable(${PROJECT_NAME} WIN32 ${SRC_LIST} ${HEADERS} ${RESOURCE})
    add_custom_command(TARGET ${PROJECT_NAME}
        POST_BUILD
        COMMAND ${DEPLOYQT_EXECUTABLE} "$<TARGET_FILE:${PROJECT_NAME}>"
            --$<LOWER_CASE:$<CONFIG>> --qmldir ${CMAKE_SOURCE_DIR}
    )
endif()

#如果是苹果系统，可能还需要MacExtras库
if(APPLE)
    set(PRIVATE_LD_FLAGS ${PRIVATE_LD_FLAGS} Qt5::MacExtras)
endif()

#链接依赖库
target_compile_definitions(${PROJECT_NAME} PRIVATE $<$<OR:$<CONFIG:Debug>,$<CONFIG:RelWithDebInfo>>:QT_QML_DEBUG>)
target_link_libraries(${PROJECT_NAME} PRIVATE Qt5::Core Qt5::Quick)
